/**
 * UserAgentFactory.java
 * 
 * Copyright (c) 2004-2020 UC Mobile Ltd.
 * F/4,Building B,Guangzhou Xinxigang,No.16 Keyun Rd,Guangzhou,China
 * All right reserved.
 * 
 * Created on 2012-10-25
 */
package com.uc.browser.useragent;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <b>UserAgentFactory简介:</b>
 * <p>通过HTTP Header User-Agent，X-UCBrowser-UA字段生成对应的对象模型</p>
 *
 * <b>功能描述:</b>
 * <p>相关的字符串分析等功能</p>
 *
 * @author raorq
 * @version 1.0
 */
public class UserAgentFactory {
    
    public static UserAgent createUserAgent(String userAgent){
        return parseUserAgent(userAgent);
    }

    public static XUCBrowserUserAgent createXUCBrowserUserAgent(String xUCBrowserUserAgent){
        return parseXUCBrowserUserAgent(xUCBrowserUserAgent);
    }
    
    
    /**
     * 
     * <p>1. 解析XUCBrowserUserAgent</p>
     * <p>2. 对数据进行封装为对象模型</p>
     * <p>格式： Key1(Value1);Key2(Value2);...</p>
     * <p>例如：例如： pf(Java);er(U);la(zh-CN);up(U2/1.0.0);re(U2/1.0.0);dv(Nokia6300);pr(UCBrowser/8.3.0.182);ov(S40 V3);pi(320*240);de(320*240);pm(1);bv(1);nm(0);im(1);sr(2);bt(UC);</p>
     * @param xUCBrowserUserAgent
     * @return XUCBrowserUserAgent
     */
    public static XUCBrowserUserAgent parseXUCBrowserUserAgent(String xUCBrowserUserAgent){
        
        XUCBrowserUserAgent ua = new XUCBrowserUserAgent();
        // ua 为null标识ua不可用
        if(xUCBrowserUserAgent == null || xUCBrowserUserAgent.isEmpty()){
            ua.setEmpty(true);
            ua.setUnkown(true);
            return ua;
        }
        
        try{
            ua.setxUCBrowserUserAgent(xUCBrowserUserAgent);
            String[] keyValueItems = xUCBrowserUserAgent.split(";");
            for(int j=0; j < keyValueItems.length; j++){
            	//add by huqiang 20130514 begin
                Pattern pattern = Pattern.compile("^([a-z]+[(][\\s\\S]+[)])$");
                if(!pattern.matcher(keyValueItems[j].toLowerCase()).find()) {
                    ua.setUnkown(true);
                    return ua;
                }
                //add by huqiang 20130514 end
                String text = keyValueItems[j];
                int startIndex = text.indexOf('(');
                int endIndex = text.indexOf(')');
                String key = text.substring(0, startIndex).toLowerCase();
                String value = text.substring(startIndex + 1, endIndex);

                if(key.equals("pf")){
                    ua.setPlatform(value);
                }else if(key.equals("la")){
                    ua.setLanguage(value);
                }else if(key.equals("er")){
                    ua.setEncryption(value);
                }else if(key.equals("re")){
                    int index = value.indexOf('/');
                    ua.setRenderingEngine(value.substring(0,index));
                    ua.setRenderingEngineVersion(value.substring(index + 1, value.length()));  
                }else if(key.equals("up")){
                    int index = value.indexOf('/');
                    ua.setUcPlatform(value.substring(0,index));
                    ua.setUcPlatformVersion(value.substring(index + 1, value.length()));  
                }else if(key.equals("dv")){
                    ua.setDevice(value);
                }else if(key.equals("pr")){
                    int index = value.indexOf('/');
                    ua.setProduct(value.substring(0,index));
                    ua.setProductVersion(value.substring(index + 1, value.length()));              
                }else if(key.equals("ov")){
                    int index = value.indexOf(' ');
                    ua.setOs(value.substring(0,index));
                    ua.setOsVersion(value.substring(index + 1, value.length()));        
                }else if(key.equals("pi")){
                    ua.setPixel(value);
                }else if(key.equals("ss")){
                    ua.setDensity(value);
                }else if(key.equals("pm")){
                    ua.setProxyMode(value);
                }else if(key.equals("bv")){
                    ua.setBrowsingView(value);
                }else if(key.equals("nm")){
                    ua.setNightMode(value);
                }else if(key.equals("im")){
                    ua.setNoImageMode(value);
                }else if(key.equals("sr")){
                    ua.setScreenRotation(value);
                }else if(key.equals("nt")){
                	ua.setNetType(value);
                }else if(key.equals("bt")){
                	ua.setBtype(value);
                }
        }
        }catch (Exception e) {
            ua.setUnkown(true);
            e.printStackTrace();
        }
        
        return ua;
    }
    
    /**
     * 
     * <p>解析user-agent字段，分析为标准对象模型</p>
     * 
     * <p>新UA自有内核规范：Company/Version (Platform; Encryption; OS Version; Language; Device) Rendering Engine/Version (Rendering Details) Product/Version UC Platform/Version Extensions</p>
     * <p>例如：                        Mozilla/5.0(Linux; U; Android 2.3; zh-CN; MI-ONEPlus) AppleWebKit/534.13 (KHTML, like Gecko) UCBrowser/8.6.0.199 U3/0.8.0 Safari/534.13</p>
     * <p>新UA自带内核规范：OBUA Product/Version UC Platform/Version Extensions。其中OBUA为自带浏览器UA</p>
     * <p>例如：                        Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5 UCBrowser/8.6.0.199 U2/1.0.0 Mobile</p>
     * <p>例如：                        Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; Microsoft; XDeviceEmulator) UCBrowser/8.6.0.199 U2/1.0.0 Mobile"</p>
     * <p>旧UA规范：               Device UA/UCWEB Version/PFID/BID。Devic UA为设备型号</p>
     * <p>例如：　　                NOKIA E71 RM-346/UCWEB6.7.1.1/28/999</p>
     * <p></p>
     *
     * @param userAgent
     * @return UserAgent
     */
    public static UserAgent parseUserAgent(String userAgent){
       
        UserAgent ua = new UserAgent();
        try {
        	// ua 为null标识ua不可用
            if(userAgent == null || userAgent.isEmpty()){
                ua.setEmpty(true);
                ua.setUnkown(true);
                return ua;
            }
            
            ua.setUserAgent(userAgent); // 设置HTTP Header最原始的User-Agent
            
            String userAgentTemp = userAgent.toLowerCase();
            // 智能机自带浏览器内核 OBUA Product/Version U2/1.0.0 Mobile
            if(userAgentTemp.indexOf(GlobalConstant.U2.toLowerCase()) != -1 && userAgentTemp.indexOf(GlobalConstant.WEBKIT.toLowerCase()) != -1){
                parseU2AndU3(userAgent, ua); 
            }
            // add by huqiang 20130514 begin
            // 兼容iphone, ipad UA自带Rendering Engine为WebKit处理
            else if(userAgentTemp.indexOf(GlobalConstant.UCBROWSER.toLowerCase()) != -1 && userAgentTemp.indexOf(GlobalConstant.WEBKIT.toLowerCase()) != -1 && userAgentTemp.indexOf(GlobalConstant.U.toLowerCase()+";") == -1){
                parseU2AndU3(userAgent, ua); 
            }
            // add by huqiang 20130514 end
            
            //兼容IE自带内核的UA情况
            else if(userAgentTemp.indexOf(GlobalConstant.U2.toLowerCase()) != -1 && userAgentTemp.indexOf(GlobalConstant.MSIE.toLowerCase()) != -1){
                parseMsieUA(userAgent, ua);
            }
            
            //最新标准UA的处理方式
            //UCWEB/2.0(iOS; U; iPh OS 4_3_2; zh-CN; iPh4) U2/1.0.0  UCBrowser/8.6.0.199 U2/1.0.0 Mobile 
            //Mozilla/5.0(Linux; U; Android 2.3; zh-CN; MI-ONEPlus) AppleWebKit/534.13 (KHTML, like Gecko) UCBrowser/8.6.0.199 U3/0.8.0 Safari/534.13
            else if(userAgentTemp.indexOf(GlobalConstant.U2.toLowerCase()) != -1 || userAgentTemp.indexOf(GlobalConstant.U3.toLowerCase()) != -1){           
                parseU2AndU3(userAgent, ua);            
            }
            // 旧有平台，Android，iPhone,WP的处理。
            else if(userAgentTemp.indexOf(GlobalConstant.J2ME.toLowerCase()) != -1){
                ua.setVersion(GlobalConstant.VERSION_1);
                ua.setProduct(GlobalConstant.UCBROWSER);
                ua.setUcPlatform(GlobalConstant.U2);
                ua.setUcPlatformVersion("1.0.0");
            } 
            // JUC (Linux;U;AndriodVersion;Language;Model;ScreenWide*ScreenHeight;) Browser Name+Browser Version/PFID/BID
            else if(userAgentTemp.indexOf(GlobalConstant.JUC.toLowerCase()) != -1){            
                parseJUC(userAgent, ua);    
            }
            else if(userAgentTemp.indexOf(GlobalConstant.IUC.toLowerCase()) != -1){
                parseIUC(userAgent, ua);    
            } else if(userAgentTemp.indexOf(GlobalConstant.UCBROWSER.toLowerCase()) != -1){
            	parseOldUCBrowserPlatform(userAgent, ua);
            } else if(userAgentTemp.indexOf(GlobalConstant._UC_BROWSER.toLowerCase()) != -1 && userAgentTemp.indexOf(GlobalConstant.MOZILLA.toLowerCase()) < 0 ){
            	parseOldUC_BrowserPlatform(userAgent, ua);
            } else if(userAgentTemp.indexOf(GlobalConstant._UC_BROWSER.toLowerCase()) != -1){
            	parseOldUCBrowserPlatform(userAgent, ua);
            } else if(userAgentTemp.indexOf(GlobalConstant.UCWEB.toLowerCase()) != -1 && userAgentTemp.indexOf(GlobalConstant.MOZILLA.toLowerCase()) != -1 ){
            	parseOldUCBrowserPlatform(userAgent, ua);
            }
            // 旧有平台的UA处理 : NOKIA E71 RM-346/UCWEB6.7.1.1/28/999
            else if(userAgentTemp.indexOf(GlobalConstant.UCWEB.toLowerCase()) != -1){
                parseOldPlatform(userAgent, ua);
            } else if(userAgentTemp.indexOf(GlobalConstant._UC.toLowerCase()) != -1){
            	parseOldUCBrowserPlatform(userAgent, ua);
            }
            // 其他非UC浏览器的UA识别，交给第3方公司自己内部处理吧。
            else{
                ua.setUnkown(true); 
            }   
		} catch (Exception e) {
			e.printStackTrace();
		}
        
        return ua;
    }

    private static void parseOldUC_BrowserPlatform(String userAgent, UserAgent ua) {
    	ua.setVersion(GlobalConstant.VERSION_1);
    	String[] values = userAgent.split("/");
    	ua.setDevice(values[0]);
    	ua.setProduct(GlobalConstant.UCBROWSER);
    	int index = 0;
    	for(int i = 0; i < values.length; i++){
    		index = values[i].toLowerCase().indexOf(GlobalConstant._UC_BROWSER.toLowerCase());
    		if( index > -1){
    			index = index + GlobalConstant._UC_BROWSER.length();
    			ua.setProductVersion(values[i].substring(index).trim());
    			break;
    		}
    	}
    }
    
    /**
     * 设置os信息
     * @param ua
     * @param os
     */
    private static void setOsInfo(UserAgent ua, String os){
    	if(os == null){
    		return;
    	}
    	String[] osItem = os.split(" ");
    	int index = 0;
    	// 如果符合UC浏览器User-Agent最新规范是可以分析出版本号，否则无法分析版本号
        if(osItem.length == 2){
            ua.setOs(osItem[0]); 
            ua.setOsVersion(osItem[1]);
        }else{
        	//查找数字
        	for(; index < os.length() && !Character.isDigit(os.charAt(index)); index++);
        	if(index == os.length()){
        		ua.setOs(os.trim()); 
        	} else {
        		ua.setOs(os.substring(0, index).trim());
        		ua.setOsVersion(os.substring(index).trim());
        	}
        }
    }
    
    private static void parseOldUCBrowserPlatform(String userAgent, UserAgent ua) {
    	ua.setVersion(GlobalConstant.VERSION_1);
    	ua.setProduct(GlobalConstant.UCBROWSER);
        String companyVersion = "";
        String platform = "";
        String encryption = "";
        String os = "";
        String language = "";
        String device = "";
        String rendering = "";
        String product = "";
        String mobile = "";
        String other = "";
        userAgent = userAgent.trim();
        int mozillaIndex = userAgent.indexOf(GlobalConstant.MOZILLA);
        if(mozillaIndex > 0){ //兼容手机底层会自动在我们的UA前面加上系统UA的问题
        	userAgent = userAgent.substring(mozillaIndex);
        }
        int startIndex = userAgent.indexOf('(');
        
        // 获取Company参数
        if(startIndex != -1){
            companyVersion = userAgent.substring(0, startIndex);
        }
        
        int endIndex = userAgent.indexOf(')');
        
        String deviceInfo =  userAgent.substring(startIndex + 1, endIndex);
        String[] deviceInfos = deviceInfo.split(";");
        try{
            platform = deviceInfos[0].trim();
            ua.setPlatform(platform);
            
            encryption = deviceInfos[1].trim();
            ua.setEncryption(encryption);
            
            int index = 2;
            
            if(deviceInfos.length > 4){
            	os = deviceInfos[index++].trim();
            	setOsInfo(ua, os);
            }
           
            
            language = deviceInfos[index++].trim();
            ua.setLanguage(language);
            
            device = deviceInfos[index++].trim();
            ua.setDevice(device);
        }catch (Exception e) { 
            e.printStackTrace();
        }
        
        try{
            String desc = userAgent.substring(endIndex + 1).trim();
            String[] descs = desc.split(" ");

            int index = -1;
            		
            if(desc.indexOf("(") > -1){
            	rendering = descs[0].trim();
            	if(GlobalConstant._UC.equalsIgnoreCase(rendering)){
            		rendering = descs[1].trim();
            	}
                index = rendering.indexOf('/');
                ua.setRenderingEngine(rendering.substring(0, index));
                endIndex = rendering.indexOf("(");
                if(endIndex < 0){
                	endIndex = rendering.length();
                }
                ua.setRenderingEngineVersion(rendering.substring(index + 1, endIndex));
            }
            
            int length = GlobalConstant.UCBROWSER.length();
            index = desc.toLowerCase().indexOf(GlobalConstant.UCBROWSER.toLowerCase());
            if(index < 0){
            	length = GlobalConstant.UCWEB.length();
                index = desc.toLowerCase().indexOf(GlobalConstant.UCWEB.toLowerCase());
            }
            if(index > -1){
            	int startVerIndex = index + length;
            	if(desc.charAt(startVerIndex) == '/'){
            		endIndex = desc.indexOf("/", startVerIndex + 1);
            		startVerIndex = startVerIndex + 1;
            	} else {
            		endIndex = desc.indexOf("/", index + 1);
            	}
            	if(endIndex > -1){
            		ua.setProductVersion(desc.substring(startVerIndex, endIndex).trim());
            	}
            }
            for(int j=1; j<descs.length; j++){ 
                if(descs[j].toLowerCase().indexOf(GlobalConstant.U2.toLowerCase()) != -1 || descs[j].toLowerCase().indexOf(GlobalConstant.U3.toLowerCase()) != -1){
                    String item = descs[j].trim();
                    index = item.indexOf('/');
                    ua.setUcPlatform(item.substring(0,index).trim());
                    ua.setUcPlatformVersion(item.substring(index + 1, item.length()).trim());
                }else if(descs[j].toLowerCase().indexOf(GlobalConstant.MOBILE.toLowerCase()) != -1){
                    ua.setMobileMode(true);
                }
            }
        }catch (Exception e) { 
            e.printStackTrace();
        }
    }
    
    private static void parseOldPlatform(String userAgent, UserAgent ua) {
        ua.setVersion(GlobalConstant.VERSION_1);
        int startIndex = userAgent.indexOf('/');
        
        String device = userAgent.substring(0, startIndex);
        ua.setDevice(device);
        ua.setProduct(GlobalConstant.UCBROWSER);
        int endIndex =  userAgent.indexOf('/', startIndex + 1);
        ua.setProductVersion(userAgent.substring(startIndex+6, endIndex));
        ua.setUcPlatform(GlobalConstant.U2);
        ua.setUcPlatformVersion("1.0.0");
    }

    private static void parseU2AndU3(String userAgent, UserAgent ua) {
        ua.setVersion(GlobalConstant.VERSION_2);
        String companyVersion = "";
        String platform = "";
        String encryption = "";
        String os = "";
        String language = "";
        String device = "";
        String rendering = "";
        String product = "";
        String mobile = "";
        String other = "";
        userAgent = userAgent.trim();
        int UCWEB_2_0Index = userAgent.indexOf("UCWEB/2.0");
        if(UCWEB_2_0Index > 0){
        	userAgent = userAgent.substring(UCWEB_2_0Index);
        }
        
        int startIndex = userAgent.indexOf('(');
        
        // 获取Company参数
        if(startIndex != -1){
            companyVersion = userAgent.substring(0, startIndex);
        }
        
        int endIndex = userAgent.indexOf(')');
        
        String deviceInfo =  userAgent.substring(startIndex + 1, endIndex);
        String[] deviceInfos = deviceInfo.split(";");
        //add by huqiang 20130514 begin
        if(deviceInfo.indexOf(GlobalConstant.U+";") == -1) {
        	try {
        		platform = deviceInfos[0].trim();
                ua.setPlatform(platform);
                
                os = deviceInfos[1].trim();
                setOsInfo(ua, os);               
                
                language = deviceInfos[2].trim();
                ua.setLanguage(language);
                
                if(deviceInfos.length >= 4){
                    device = deviceInfos[3].trim();
                    ua.setDevice(device);
                }else{
                    
                }
        	}catch(Exception e) {
        		e.printStackTrace();
        	}
        }else {
        //add by huqiang 20130514 end
	        try{
	            platform = deviceInfos[0].trim();
	            ua.setPlatform(platform);
	            
	            encryption = deviceInfos[1].trim();
	            ua.setEncryption(encryption);
	            
	            os = deviceInfos[2].trim();
	            setOsInfo(ua, os);
	           
	            
	            language = deviceInfos[3].trim();
	            ua.setLanguage(language);
	            
	            if(deviceInfos.length >= 5){
	                device = deviceInfos[4].trim();
	                ua.setDevice(device);
	            }else{
	                
	            }
	        }catch (Exception e) { 
	            e.printStackTrace();
	        }
        //add by huqiang 20130514 begin
        }
        //add by huqiang 20130514 end        
        try{
            String desc = userAgent.substring(endIndex + 1).trim();
            String[] descs = desc.split(" ");

            rendering = descs[0].trim();
            int index = rendering.indexOf('/');
            ua.setRenderingEngine(rendering.substring(0, index));
            ua.setRenderingEngineVersion(rendering.substring(index + 1, rendering.length()));
            
            for(int j=1; j<descs.length; j++){ 
                if(descs[j].toLowerCase().indexOf(GlobalConstant.UCBROWSER.toLowerCase()) != -1){
                    product = descs[j].trim();
                    index = product.indexOf('/');
                    ua.setProduct(product.substring(0,index));
                    ua.setProductVersion(product.substring(index + 1, product.length()));
                }else if(descs[j].toLowerCase().indexOf(GlobalConstant.U2.toLowerCase()) != -1 || descs[j].toLowerCase().indexOf(GlobalConstant.U3.toLowerCase()) != -1){
                    String item = descs[j].trim();
                    index = item.indexOf('/');
                    ua.setUcPlatform(item.substring(0,index));
                    ua.setUcPlatformVersion(item.substring(index + 1, item.length()));
                }else if(descs[j].toLowerCase().indexOf(GlobalConstant.MOBILE.toLowerCase()) != -1){
                    ua.setMobileMode(true);
                }
            }
        }catch (Exception e) { 
            e.printStackTrace();
        }
    }
    
    //Mozilla/5.0 (compatible; MSIE 9.0; Windows Phone OS 7.5; Trident/5.0; IEMobile/9.0; Microsoft; XDeviceEmulator)UCBrowser/8.6.0.199 U2/1.0.0 Mobile
    private static void parseMsieUA(String userAgent, UserAgent ua) {
        ua.setVersion(GlobalConstant.VERSION_2);
        String companyVersion = "";
        String platform = "";
        String encryption = "";
        String os = "";
        String language = "";
        String device = "";
        String rendering = "";
        String product = "";
        String mobile = "";
        String other = "";
        int startIndex = userAgent.indexOf('(');
        
        // 获取Company参数
        if(startIndex != -1){
            companyVersion = userAgent.substring(0, startIndex);
        }
        
        int endIndex = userAgent.indexOf(')');
        
        String deviceInfo =  userAgent.substring(startIndex + 1, endIndex);
        String[] deviceInfos = deviceInfo.split(";");
        try{
            platform = deviceInfos[2].trim();
            ua.setPlatform(platform); 
            ua.setOs(platform);   
         
            rendering = deviceInfos[3].trim();
            int index = rendering.indexOf('/');
            ua.setRenderingEngine(rendering.substring(0, index));
            ua.setRenderingEngineVersion(rendering.substring(index + 1, rendering.length()));
            
            if(deviceInfos.length > 7){
            	device = deviceInfos[7].trim();
                ua.setDevice(device);  
            }
                     
        }catch (Exception e) { 
            e.printStackTrace();
        }
        
        try{
            String desc = userAgent.substring(endIndex + 1).trim();
            String[] descs = desc.split(" ");

            int index = -1;
            
            for(int j=0; j<descs.length; j++){ 
                if(descs[j].toLowerCase().indexOf(GlobalConstant.UCBROWSER.toLowerCase()) != -1){
                    product = descs[j].trim();
                    index = product.indexOf('/');
                    ua.setProduct(product.substring(0,index));
                    ua.setProductVersion(product.substring(index + 1, product.length()));
                }else if(descs[j].toLowerCase().indexOf(GlobalConstant.U2.toLowerCase()) != -1 || descs[j].toLowerCase().indexOf(GlobalConstant.U3.toLowerCase()) != -1){
                    String item = descs[j].trim();
                    index = item.indexOf('/');
                    ua.setUcPlatform(item.substring(0,index));
                    ua.setUcPlatformVersion(item.substring(index + 1, item.length()));
                }else if(descs[j].toLowerCase().indexOf(GlobalConstant.MOBILE.toLowerCase()) != -1){
                    ua.setMobileMode(true);
                }
            }
        }catch (Exception e) { 
            e.printStackTrace();
        }
    }
    
    private static void parseIUC(String userAgent, UserAgent ua) {
        ua.setVersion(GlobalConstant.VERSION_1);
        ua.setUcPlatform(GlobalConstant.U2);
        ua.setUcPlatformVersion("1.0.0");
        ua.setProduct(GlobalConstant.UCBROWSER);
        
        String companyVersion = "";
        String platform = "";
        String encryption = "";
        String os = "";
        String language = "";
        String device = "";
        String rendering = "";
        String product = "";
        String mobile = "";
        String other = "";
        int startIndex = userAgent.indexOf('(');
        
        // 获取Company参数
        if(startIndex != -1){
            companyVersion = userAgent.substring(0, startIndex);
        }
        
        int endIndex = userAgent.indexOf(')');
        
        String deviceInfo =  userAgent.substring(startIndex + 1, endIndex);
        String[] deviceInfos = deviceInfo.split(";");
        try{
           // platform = deviceInfos[0].trim();
            ua.setPlatform("iOS");
            
            encryption = deviceInfos[0].trim();
            ua.setEncryption(encryption);
            
            os = deviceInfos[1].trim();
            setOsInfo(ua, os);
           
            
            language = deviceInfos[2].trim();
            ua.setLanguage(language);
            
//            if(deviceInfos.length >= 5){
//                device = deviceInfos[4].trim();
                ua.setDevice("iPhone");
//            }else{
//                
//            }
           
            
        }catch (Exception e) { 
            e.printStackTrace();
        }
        
        try{
            String desc = userAgent.substring(endIndex + 1).trim();
            String[] descs = desc.split(" ");
            int index = -1; 
  
            for(int j=0; j<descs.length; j++){ 
                if(descs[j].toLowerCase().indexOf(GlobalConstant.UCBROWSER.toLowerCase()) != -1){
                    ua.setProduct(GlobalConstant.UCBROWSER);
                    endIndex =  descs[j].indexOf('/');
                    ua.setProductVersion(descs[j].substring(9, endIndex));                  
                }else if(descs[j].toLowerCase().indexOf(GlobalConstant.U2.toLowerCase()) != -1 || descs[j].toLowerCase().indexOf(GlobalConstant.U3.toLowerCase()) != -1){
                    String item = descs[j].trim();
                    index = item.indexOf('/');
                    ua.setUcPlatform(item.substring(0,index));
                    ua.setUcPlatformVersion(item.substring(index + 1, item.length()));
                }else if(descs[j].toLowerCase().indexOf(GlobalConstant.MOBILE.toLowerCase()) != -1){
                    ua.setMobileMode(true);
                }else if(descs[j].toLowerCase().indexOf(GlobalConstant.WEBKIT.toLowerCase()) != -1 || descs[j].toLowerCase().indexOf(GlobalConstant.MSIE.toLowerCase()) != -1){
                    rendering = descs[0].trim();
                    index = rendering.indexOf('/');
                    ua.setRenderingEngine(rendering.substring(0, index));
                    ua.setRenderingEngineVersion(rendering.substring(index + 1, rendering.length()));
                }
            }
        }catch (Exception e) { 
            e.printStackTrace();
        }
    }
    
    private static void parseJUC(String userAgent, UserAgent ua) {
        ua.setVersion(GlobalConstant.VERSION_1);
        ua.setUcPlatform(GlobalConstant.U2);
        ua.setUcPlatformVersion("1.0.0");
        ua.setProduct(GlobalConstant.UCBROWSER);
        
        String companyVersion = "";
        String platform = "";
        String encryption = "";
        String os = "";
        String language = "";
        String device = "";
        String rendering = "";
        String product = "";
        String mobile = "";
        String other = "";
        int startIndex = userAgent.indexOf('(');
        
        // 获取Company参数
        if(startIndex != -1){
            companyVersion = userAgent.substring(0, startIndex);
        }
        
        int endIndex = userAgent.indexOf(')');
        
        String deviceInfo =  userAgent.substring(startIndex + 1, endIndex);
        String[] deviceInfos = deviceInfo.split(";");
        try{
            platform = deviceInfos[0].trim();
            ua.setPlatform(platform);
            
            encryption = deviceInfos[1].trim();
            ua.setEncryption(encryption);
            
            os = deviceInfos[2].trim();
            setOsInfo(ua, os);
           
            
            language = deviceInfos[3].trim();
            ua.setLanguage(language);
            
            if(deviceInfos.length >= 5){
                device = deviceInfos[4].trim();
                ua.setDevice(device);
            }else{
                
            }
           
            
        }catch (Exception e) { 
            e.printStackTrace();
        }
        
        try{
            String desc = userAgent.substring(endIndex + 1).trim();
            String[] descs = desc.split(" ");
            int index = -1;
//            rendering = descs[0].trim();
//            int index = rendering.indexOf('/');
//            ua.setRenderingEngine(rendering.substring(0, index));
//            ua.setRenderingEngineVersion(rendering.substring(index + 1, rendering.length()));
//            
            for(int j=0; j<descs.length; j++){ 
                if(descs[j].toLowerCase().indexOf(GlobalConstant.UCBROWSER.toLowerCase()) != -1 || descs[j].toLowerCase().indexOf(GlobalConstant.UCWEB.toLowerCase()) != -1 ){
                    ua.setProduct(GlobalConstant.UCBROWSER);
                    int length = descs[j].toLowerCase().indexOf(GlobalConstant.UCBROWSER.toLowerCase()) != -1 ? GlobalConstant.UCBROWSER.length() : GlobalConstant.UCWEB.length();
                    index = length;
                    if(descs[j].charAt(length) == '/'){
                    	endIndex =  descs[j].indexOf('/', length + 1);
                    	index++;
                    } else {
                    	endIndex =  descs[j].indexOf('/');
                    }
                    ua.setProductVersion(descs[j].substring(index, endIndex));                  
                }else if(descs[j].toLowerCase().indexOf(GlobalConstant.U2.toLowerCase()) != -1 || descs[j].toLowerCase().indexOf(GlobalConstant.U3.toLowerCase()) != -1){
                    String item = descs[j].trim();
                    index = item.indexOf('/');
                    ua.setUcPlatform(item.substring(0,index));
                    ua.setUcPlatformVersion(item.substring(index + 1, item.length()));
                }else if(descs[j].toLowerCase().indexOf(GlobalConstant.MOBILE.toLowerCase()) != -1){
                    ua.setMobileMode(true);
                }else if(descs[j].toLowerCase().indexOf(GlobalConstant.WEBKIT.toLowerCase()) != -1 || descs[j].toLowerCase().indexOf(GlobalConstant.MSIE.toLowerCase()) != -1){
                    rendering = descs[0].trim();
                    index = rendering.indexOf('/');
                    ua.setRenderingEngine(rendering.substring(0, index));
                    ua.setRenderingEngineVersion(rendering.substring(index + 1, rendering.length()));
                }
            }
        }catch (Exception e) { 
            e.printStackTrace();
        }
    }
    
}
